<template>
  <!--招聘规划-->
  <div class="bf-commont-template">
    <div class="clearfix bf-top-button">
      <div class="float-l">
        <el-button @click="showAdd" type="primary">制定规划</el-button>
      </div>
      <div class="float-r">
        <div class="float-l">
          <el-input placeholder="请输入名称搜索内容" prefix-icon="el-icon-search" v-model="searchData" @keyup.enter.native="quickSearch"></el-input>
        </div>
        <div class="float-l padding-l">
          <el-button @click="showSearch" type="primary">高级筛选</el-button>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick" class="margin-l margin-r">
      <el-tab-pane :label="'待提交'+ tabs.waitVett" name="waitVett">
        <bf-table :data="userData" :setting="setting" :rowAction="waitRowAction" @editSubmit="editSubmit" @pageChange="pageChange" :editRules="editRules" ref="tableA"></bf-table>
      </el-tab-pane>
      <el-tab-pane :label="'审批中'+ tabs.approve" name="approve">
        <bf-table :data="userData" :setting="setting" :rowAction="rowAction" @editSubmit="editSubmit" @pageChange="pageChange" :editRules="editRules" ref="table"></bf-table>
      </el-tab-pane>
      <el-tab-pane :label="'已审批'+ tabs.vetted" name="vetted">
        <bf-table :data="userData" :setting="setting" :rowAction="rowAction" @editSubmit="editSubmit" @pageChange="pageChange" :editRules="editRules" ref="table"></bf-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 添加, 制定规划 -->
    <bf-table-add :width="600" :editForm='addSetting' :title="'新增'" :submitButtons="submitButtons" ref='addRosterRef' @formSubmit='addRosterSubmit' :editRules="addPlanRules">
      <template slot="table">
        <div class="bf-table-title clearfix">
          <div>招聘人员详情</div>
          <div @click="addPlan">新增</div>
        </div>
        <bf-table :data="planData" :setting="planSetting" :rowAction="planRowAction" :editRules="editRules" :hiddenPagination="true" ref="table"></bf-table>
      </template>
    </bf-table-add>
    <!-- 高级搜索 -->
    <bf-table-search
      :editForm='searchSetting'
      :title="'高级搜索'"
      ref='searchRosterRef'
      @formSubmit='searchRosterSubmit'
    ></bf-table-search>

    <!-- 编辑 -->
    <bf-table-edit :editForm='editSetting' :submitButtons="editSubmitButtons" :title="'编辑'" ref='convertFullRosterRef' @formSubmit='convertFullRosterSubmit' :editRules="addPlanRules">
      <template slot="table">
        <div class="bf-table-title clearfix">
          <div>招聘人员详情</div>
          <div @click="editPlan">新增</div>
        </div>
        <bf-table :data="editPlanData" :setting="planSetting" :rowAction="editPlanRowAction" :editRules="editRules" :hiddenPagination="true" ref="table"></bf-table>
      </template>
    </bf-table-edit>
    <!--详情-->
    <bf-table-detail ref="bfTableDetail" :title="'规划详情'" :editForm="editSetting" :showHistory='true' :approveSetting='approveSetting1'>
      <template slot="table">
        <div class="bf-table-title clearfix">
          <div>招聘人员详情:</div>
          <div> </div>
        </div>
        <bf-table :data="editPlanData" :setting="planSetting" :editRules="editRules" :hiddenPagination="true" ref="table"></bf-table>
      </template>

    </bf-table-detail>
    <!-- 新增-新增 -->
    <bf-dialog-form ref="addDialogFormAdd" :editRules="planRules" :editForm="planSetting" :title="'新增规划'" @formSubmit="addPlanAdd"></bf-dialog-form>
    <!-- 新增-编辑 -->
    <bf-dialog-form ref="addEditDialogFormEdit" :editRules="planRules" :editForm="editPlanSetting" :title="'编辑规划'" @formSubmit="addPlanEdit"></bf-dialog-form>

    <!-- 编辑-新增 -->
    <bf-dialog-form ref="editDialogFormAdd" :editRules="planRules" :editForm="planSetting" :title="'新增规划'" @formSubmit="editPlanAdd"></bf-dialog-form>
    <!-- 编辑-编辑 -->
    <bf-dialog-form ref="editEditdialogFormEdit" :editRules="planRules" :editForm="editPlanSetting" :title="'编辑规划'" @formSubmit="editPlanEdit"></bf-dialog-form>
  </div>
</template>

<script>
import { getRequireList, getdetail, addplan, updatePlan, findStatusCount, deleteplan, updateBackSubmit, giveupPlan, getPosition } from './api.js'
import { setting, addSetting, editSetting, searchSetting } from './table.setting.js'
// import { getFilesListApi } from '../../../../api/common'
export default {
  data() {
    return {
      searchSetting: searchSetting,
      approveSetting1: {
        procDefKey: 'recruit-plan',
        businessKey: ''
      },
      planData: [],
      editPlanData: [],
      editingPlan: {},
      planRules: {
        position: [
          { required: true, message: '必填项', trigger: 'change' }
        ],
        recruitsNumbers: [
          { required: true, message: '必填项', trigger: 'change' }
        ]
      },
      editPlanRowAction: [ // 编辑
        {
          label: '编辑',
          click: (item) => {
            this.editingPlan = item.scope.row
            this.$refs.editEditdialogFormEdit.show(item.scope.row)
          }
        },
        {
          label: '删除',
          click: (item) => {
            this.editPlanData.forEach((v, index) => {
              if (v.position === item.scope.row.position) {
                this.editPlanData.splice(index, 1)
              }
            })
            this.planData.forEach((v, index) => {
              if (v.position === item.scope.row.position) {
                this.planData.splice(index, 1)
              }
            })
          }
        }
      ],
      planRowAction: [ // 新增
        {
          label: '编辑',
          click: (item) => {
            this.editingPlan = item.scope.row
            if (this.$refs.addRosterRef.form.orgId) {
              const params = {
                orgId: this.$refs.addRosterRef.form.orgId
              }
              const options = []
              getPosition(params).then((response) => { // 根据部门查询岗位
                if (response.data !== undefined) {
                  if (response.success) {
                    response.data.forEach((v, index) => {
                      const obj = {
                        value: v.postId,
                        name: v.postName
                      }
                      options.push(obj)
                    })
                    this.editPlanSetting.forEach(v => {
                      if (v.field === 'position') {
                        v.options = options
                      }
                    })
                  }
                }
              })
            }
            this.$refs.addEditDialogFormEdit.show(item.scope.row)
          }
        },
        {
          label: '删除',
          click: (item) => {
            this.planData.forEach((v, index) => {
              if (v.position === item.scope.row.position) {
                this.planData.splice(index, 1)
              }
            })
            this.editPlanData.forEach((v, index) => {
              if (v.position === item.scope.row.position) {
                this.editPlanData.splice(index, 1)
              }
            })
          }
        }
      ],

      planSetting: [
        {
          field: 'position',
          title: '岗位',
          type: 'selected',
          options: [

          ]
        },
        {
          field: 'recruitsNumbers',
          title: '招聘人数',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }
      ],
      editPlanSetting: [
        {
          field: 'position',
          title: '岗位',
          type: 'selected',
          options: [],
          disableEdit: true
        },
        {
          field: 'recruitsNumbers',
          title: '招聘人数',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }
      ],
      searchData: '',
      /**
       * 新增的setting
       */
      setting: setting,
      addSetting: addSetting,
      editSetting: editSetting,
      submitButtons: [
        {
          title: '提交',
          click: (form) => {
            form.status = 1
            form.action = 1
            this.planData
            // console.log(this.planData)
            form.detailList = this.planData
            if (form.detailList.length === 0 || form.detailList === undefined) {
              this.$message({
                message: '招聘信息不能为空',
                type: 'error'
              })
              return false
            } else {
              this.addRosterSubmit(form)
            }
          }
        },
        {
          title: '暂存',
          click: (form) => {
            form.status = 0
            form.action = 1
            form.detailList = this.planData
            if (form.detailList.length === 0 || form.detailList === undefined) {
              this.$message({
                message: '招聘信息不能为空',
                type: 'error'
              })
              return false
            } else {
              this.addRosterSubmit(form)
            }
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.addRosterRef.hiden()
          }
        }
      ],
      editSubmitButtons: [ // 编辑保存

        {
          title: '提交',
          click: (form) => { // 编辑提交
            if (form.detailList.length === 0 || form.detailList === undefined) {
              this.$message({
                message: '招聘信息不能为空',
                type: 'error'
              })
              return false
            } else {
              if (form.status === '0') {
                form.status = '1'
                form.action = 1

                updatePlan(form).then((response) => {
                  if (response.success) {
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    })
                    this.getTabs()
                    this.searchList('')
                    this.$refs.convertFullRosterRef.hiden()
                  }
                })
              } else {
                form.status = '1'
                form.action = 1

                updateBackSubmit(form).then((response) => {
                  if (response.success) {
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    })
                    this.getTabs()
                    this.searchList('', this.getActivityStatus())
                    this.$refs.convertFullRosterRef.hiden()
                  }
                })
              }
            }
          }
        },
        {
          title: '暂存',
          click: (form) => {
            if (form.detailList.length === 0 || form.detailList === undefined) {
              this.$message({
                message: '招聘信息不能为空',
                type: 'error'
              })
              return false
            } else {
              this.convertFullRosterSubmit(form)
            }
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.addRosterRef.hiden()
            this.$refs.convertFullRosterRef.hiden()
          }
        }
      ],
      userData: {
      },
      tabs: {
        waitVett: 0,
        approve: 0,
        vetted: 0
      },
      statusData: {},
      activeName: 'waitVett',
      waitRowAction: [
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.planId
            const params = {
              orgId: item.scope.row.orgId
            }
            const options = []
            getPosition(params).then((response) => { // 根据部门查询岗位
              if (response.data !== undefined) {
                if (response.success) {
                  response.data.forEach((v, index) => {
                    const obj = {
                      value: v.postId,
                      name: v.postName
                    }
                    options.push(obj)
                  })
                  this.planSetting.forEach(v => {
                    if (v.field === 'position') {
                      v.options = options
                    }
                  })
                }
              }
            })
            this.findDetailById(item.scope.row.planId, this.$refs.bfTableDetail.show)
          }
        },
        {
          label: '编辑',
          click: (item) => {
            if ((item.scope.row.taskDefKey === 'modify' || item.scope.row.taskDefKey === undefined) && (item.scope.row.status === '0' || item.scope.row.status === '1')) {
              const params = {
                orgId: item.scope.row.orgId
              }
              const options = []
              getPosition(params).then((response) => { // 根据部门查询岗位
                if (response.data !== undefined) {
                  if (response.success) {
                    // console.log(response.data)
                    response.data.forEach((v, index) => {
                      const obj = {
                        value: v.postId,
                        name: v.postName
                      }
                      options.push(obj)
                    })
                    this.planSetting.forEach(v => {
                      if (v.field === 'position') {
                        v.options = options
                      }
                    })
                    this.editPlanSetting.forEach(v => {
                      if (v.field === 'position') {
                        v.options = options
                      }
                    })
                  }
                }
              })

              this.findDetailById(item.scope.row.planId, this.$refs.convertFullRosterRef.show)
            } else {
              this.$message({
                message: '该阶段不可编辑',
                type: 'error'
              })
              return false
            }
          }
        },
        {
          label: '提交',
          click: (item) => {
            if (item.scope.row.taskDefKey === 'modify' || item.scope.row.taskDefKey === undefined) {
              // 查询附件信息
              // const params = {
              //   busiKey: this.form[busiKey],
              //   busiFunc: this.item.busiFunc ? this.item.busiFunc : this.form.busiFunc
              // }
              // getFilesListApi(params).then(response => {
              //   if(response.success) {
              //     console.log(response)
              //   }
              // })
              this.planSubmit(item.scope.row.planId)
            } else {
              this.$message({
                message: '该阶段不可编辑',
                type: 'error'
              })
              return false
            }
          }
        },
        {
          label: '删除',
          hiddenStatus: (row) => {
            if (row.status !== '0') {
              return false
            } else {
              return true
            }
          },
          click: (item) => {
            if (item.scope.row.status === '0') {
              this.$refs.tableA.showDelete(item)
            } else {
              this.$message({
                message: '退回修改状态不可删除',
                type: 'error'
              })
              return false
            }
          }
        },
        {
          label: '放弃申请',
          disableStatus: (row) => {
            if (row.status == '1' && row.taskDefKey == 'modify') {
              return false
            } else {
              return true
            }
          },
          hiddenStatus: (row) => {
            if (row.status == '1' && row.taskDefKey == 'modify') {
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            if (item.scope.row.taskDefKey === 'modify' && item.scope.row.status === '1') {
              this.giveup(item.scope.row.planId)
            } else {
              this.$message({
                message: '未发起申请，可直接删除',
                type: 'error'
              })
            }
          }
        }
      ],

      rowAction: [
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.planId
            const params = {
              orgId: item.scope.row.orgId
            }
            const options = []
            getPosition(params).then((response) => { // 根据部门查询岗位
              if (response.data !== undefined) {
                if (response.success) {
                  response.data.forEach((v, index) => {
                    const obj = {
                      value: v.postId,
                      name: v.postName
                    }
                    options.push(obj)
                  })
                  this.planSetting.forEach(v => {
                    if (v.field === 'position') {
                      v.options = options
                    }
                  })
                }
              }
            })

            this.findDetailById(item.scope.row.planId, this.$refs.bfTableDetail.show)
          }
        }
      ],
      addPlanRules: { // 新增的验证规则
        planName: [
          { required: true, message: '请输入规划名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50个字符', trigger: 'blur' }
        ],
        orgId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '请选择计划开始日期', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '请选择计划结束日期', trigger: 'blur' }
        ],
        detailList: [
          { required: true, message: '请选择计划结束日期', trigger: 'blur' }
        ]
      },
      editRules: {
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20个字符', trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '请输入用户角色', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20个字符', trigger: 'blur' }
        ]
      },
      searchParams: { // 搜索条件
        page: 1
      }
    }
  },
  mounted: function() {
    this.searchList('')
    this.getTabs()
  },
  methods: {
    editPlanEdit(form) {
      this.editPlanData.forEach((v, index) => {
        if (v.position == form.position) {
          const obj = this.editPlanData[index]
          obj.recruitsNumbers = form.recruitsNumbers
          obj.remarks = form.remarks
        }
      })
      this.$refs.editEditdialogFormEdit.hidden()
    },
    addPlanEdit(form) {
      // const isDouble = false
      this.planData.forEach((v, index) => {
        if (v.position == form.position) {
          const obj = this.planData[index]
          obj.recruitsNumbers = form.recruitsNumbers
          obj.remarks = form.remarks
        }

      })
      this.$refs.addEditDialogFormEdit.hidden()
    },
    addPlanAdd(form) {
      // console.log(form)
      let isDouble = false
      this.planData.some(v => {
        if (v.position == form.position) {
          isDouble = true
          return true
        }
      })
      if (isDouble) {
        this.$message({
          message: '岗位已经存在，请新增其他岗位',
          type: 'error'
        })
      } else {
        this.planData.push(form)
        this.$refs.addDialogFormAdd.hidden()
      }
    },
    editPlanAdd(form) {
      // console.log(form)
      let isDouble = false

      this.editPlanData.some(v => {
        if (v.position == form.position) {
          isDouble = true
          return true
        }
      })
      if (isDouble) {
        this.$message({
          message: '岗位已经存在，请新增其他岗位',
          type: 'error'
        })
      } else {
        this.editPlanData.push(form)
        this.$refs.editDialogFormAdd.hidden()
      }
    },
    addPlan(form) {
      const options = []
      const orgId = this.$refs.addRosterRef.form.orgId
      if (!orgId) {
        this.$message({
          message: '请先选择部门信息',
          type: 'error'
        })
        return false
      } else {
        const params = {
          orgId: orgId
        }
        getPosition(params).then((response) => { // 根据部门查询岗位
          if (response.data !== undefined) {
            if (response.success) {
              // console.log(response.data)
              response.data.forEach((v, index) => {
                const obj = {
                  value: v.postId,
                  name: v.postName
                }
                options.push(obj)
              })
              this.planSetting.forEach(v => {
                if (v.field === 'position') {
                  v.options = options
                }
              })
            let obj = {
              position:'',
              remarks: '',
              recruitsNumbers: ''
            }
              this.$refs.addDialogFormAdd.show(obj)
            }
          } else {
            this.$message({
              message: '请先选择部门信息',
              type: 'error'
            })
            return false
          }
        })
      }
    },
    editPlan(form) {
      const options = []
      const orgId = this.$refs.convertFullRosterRef.form.orgId
      if (!orgId) {
        this.$message({
          message: '请先选择部门信息',
          type: 'error'
        })
        return false
      } else {
        const params = {
          orgId: orgId
        }
        getPosition(params).then((response) => { // 根据部门查询岗位
          if (response.data !== undefined) {
            if (response.success) {
              // console.log(response.data)
              response.data.forEach((v, index) => {
                const obj = {
                  value: v.postId,
                  name: v.postName
                }
                options.push(obj)
              })
              this.planSetting.forEach(v => {
                if (v.field === 'position') {
                  v.options = options
                }
              })
              this.$refs.editDialogFormAdd.show()
            }
          } else {
            this.$message({
              message: '请先选择部门信息',
              type: 'error'
            })
            return false
          }
        })
      }
    },
    giveup(planId) {
      getdetail(planId).then((response) => {
        giveupPlan(response.data).then((response) => { // 放弃申请
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.searchList('')
            this.getTabs()
          }
        })
      })
    },
    findDetailById(id, showCall) { // 详情
      getdetail(id).then((response) => {
        this.editPlanData = response.data.detailList
        showCall(response.data)
      })
    },
    searchList(pageParams) {
      const params = this.searchParams
      params.status = this.getActivityStatus()
      getRequireList(params).then((response) => {
        this.userData = response
      }).catch((response) => {
      })
    },

    getActivityStatus() {
      if (this.activeName === 'waitVett') {
        return 0;
      }
      if (this.activeName === 'approve') {
        return 1;
      }
      if (this.activeName === 'vetted') {
        return 2;
      }
    },

    editSubmit(params, formType, formList) {
      if (formType === 'delete') {
        const planId = params.row.planId
        this.deleteItem(planId)
      }
    },

    deleteItem(planId) {
      deleteplan(planId).then((response) => {
        if (response.success) {
          this.$message({
            message: '删除成功!',
            type: 'success'
          })
          this.searchList('')
          this.getTabs()
        }
      })
    },


    planSubmit(id) { // 提交
      getdetail(id).then((response) => {
        if (response.data.status === '0') {
          response.data.status = '1'
          updatePlan(response.data).then((response) => {
            if (response.success) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.getTabs()
              this.searchList('')
              this.$refs.convertFullRosterRef.hiden()
            }
          })
        } else {
          response.data.action = 1
          updateBackSubmit(response.data).then((response) => {
            if (response.success) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.getTabs()
              this.searchList('', this.getActivityStatus())
              this.$refs.convertFullRosterRef.hiden()
            }
          })
        }
      })
    },
    addRosterSubmit(form) { // 制定规划
      addplan(form).then((response) => {
        if (response.success) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.$refs.addRosterRef.hiden()
          this.searchList('')
          this.getTabs()
        }
      })
    },
    searchRosterSubmit(form) { // 高级搜索
      this.searchParams.orgId = form.orgId
      this.searchParams.status = form.status
      this.searchParams.planId = form.planId
      this.searchParams.planName = form.planName
      this.searchList()
      this.$refs.searchRosterRef.hiden()
    },
    convertFullRosterSubmit(form) { // 编辑保存
      if (form.status === '0') {
        form.action = 1
        updatePlan(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$refs.convertFullRosterRef.hiden()
            this.searchList('')
          }
        })
      } else {
        form.status = 0
        form.action = 1
        updateBackSubmit(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$refs.convertFullRosterRef.hiden()
            this.searchList('')
            this.getTabs()
          }
        })
      }
    },
    editPlanSubmit(form) {
      form.status
      if (form.status === '0') {
        form.status = '1'
        form.action = 1
        updatePlan(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$refs.convertFullRosterRef.hiden()
            this.searchList('')
          }
        })
      } else {
        form.status = '1'
        form.action = 1
        updateBackSubmit(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$refs.convertFullRosterRef.hiden()
            this.searchList('')
          }
        })
      }
    },
    getTabs() {
      this.tabs.waitVett = 0
      this.tabs.approve = 0
      this.tabs.vetted = 0
      findStatusCount().then((request) => {
        this.statusData = request.data
        this.tabs.waitVett = this.statusData.count4Status0
        this.tabs.approve = this.statusData.count4Status1
        this.tabs.vetted = this.statusData.count4Status2
      })
    },

    pageChange(pageObj) {
      this.searchParams.page = pageObj.page
      this.searchParams.pageSize = pageObj.pageSize
      this.searchList()
    },
    showSearch() {
      this.$refs.searchRosterRef.show()
    },
    showAdd() {
      this.planData = []
      this.editPlanData = []
      this.$refs.addRosterRef.show()
    },
    quickSearch() {
      // 快速搜索
      this.searchParams.page = 1
      this.searchParams.planName = this.searchData
      this.searchList()
    },
    handleClick(tab, event) {
      this.searchParams.page = 1
      this.searchList()
    }
  }
}
</script>

<style scoped>

</style>
